import 'dart:convert';

import 'package:dio/dio.dart';
import 'package:excel_utils/pages/week/bean/AccountsReceivableBean.dart';
import 'package:excel_utils/pages/week/bean/AdjustInvoiceBean.dart';
import 'package:excel_utils/pages/week/bean/CustomerAdditionBean.dart';
import 'package:excel_utils/pages/week/bean/OtherOnlineRetailersBean.dart';
import 'package:excel_utils/repository/constant.dart';
import 'package:excel_utils/service/DioManager.dart';

import '../../pages/daily/daily_report/bean/CmcTicketBean.dart';
import '../../pages/daily/daily_report/bean/CustomerBean.dart';
import '../../pages/daily/daily_report/bean/DailyInformationBean.dart';
import '../../pages/daily/daily_report/bean/DailyVoteBean.dart';
import '../../pages/daily/daily_report/bean/IndependentStatisticalBean.dart';
import '../../pages/daily/daily_report/bean/LocalDeliveryStatisticsBean.dart';
import '../../pages/daily/daily_report/bean/OtherTopThirtyBean.dart';
import '../../pages/daily/daily_report/bean/SignStatisticsBean.dart';
import '../../pages/daily/daily_report/bean/ThirdPartyBean.dart';

abstract class IK8Repository {
  Future<LocalDeliveryStatisticsBean> localDeliveryStatisticsVotingData({dynamic maps});
  Future<IndependentStatisticalBean> independentStatisticalIdentificationData({dynamic maps});
  Future<OtherTopThirtyBean> otherTopThirtyData({dynamic maps});
  Future<ThirdPartyBean> thirdPartyData({dynamic maps});
  Future<CmsTicketBean> cmsTicketData({dynamic maps});
  Future<DailyVoteBean> dailyVoteData({dynamic maps});
  Future<DailyInformationBean> dailyInfoData({dynamic maps});
  Future<SignStatisticsBean> querySignStatisticsForDataUrl({dynamic maps});
  Future<CustomerBean> queryCustomerData({dynamic maps});
  // 客户添加终止报告
  Future<CustomerAdditionBean> customerAdditionTerminateReportData({dynamic maps});
  // 其他电商比较表
  Future<OtherOnlineRetailersBean> otherOnlineRetailersComparisonData({dynamic maps});
  // 账单录入信息
  Future<AccountsReceivableBean> accountsReceivableInfoData({dynamic maps});
  // 调整发票
  Future<AdjustInvoiceBean> adjustInvoiceData({dynamic maps});
}

class K8Repository implements IK8Repository {
  DioManager dioManager = DioManager();

  @override
  Future<LocalDeliveryStatisticsBean> localDeliveryStatisticsVotingData({dynamic maps}) async {
    FormData formData = FormData.fromMap(maps);
    Response<dynamic> cmsData = await dioManager.post(path: Constant.localDeliveryStatisticsDataUrl, options: Options(headers: {"content-type": "form-data"}), data: formData);
    LocalDeliveryStatisticsBean cmsTicketBean = LocalDeliveryStatisticsBean.fromJson(json.decode(cmsData.data));
    return cmsTicketBean;
  }

  @override
  Future<IndependentStatisticalBean> independentStatisticalIdentificationData({maps}) async {
    FormData formData = FormData.fromMap(maps);
    Response<dynamic> cmsData = await dioManager.post(path: Constant.independentStatisticalIdentificationDataUrl, options: Options(headers: {"content-type": "form-data"}), data: formData);
    IndependentStatisticalBean independentStatisticalBean = IndependentStatisticalBean.fromJson(json.decode(cmsData.data));
    if (independentStatisticalBean.data?.isNotEmpty ?? false) {
      for (var i = 0; i < independentStatisticalBean.data!.length; i++) {
        int total = 0;
        independentStatisticalBean.data?[i].total = 0;
        var dataMap = independentStatisticalBean.data![i].toJson();
        dataMap.forEach((key, value) {
          if (key != "dateTime" && key != "Total" && key != "LINEX" && key != "RINCOSAIR") {
            value ??= "0";
            total += int.parse(value.toString());
          }
        });
        independentStatisticalBean.data?[i].total = total;
      }
    }
    return independentStatisticalBean;
  }

  @override
  Future<OtherTopThirtyBean> otherTopThirtyData({maps}) async {
    FormData formData = FormData.fromMap(maps);
    Response<dynamic> cmsData = await dioManager.post(path: Constant.otherTopThirtyDataUrl, options: Options(headers: {"content-type": "form-data"}), data: formData);
    OtherTopThirtyBean otherTopThirtyBean = OtherTopThirtyBean.fromJson(json.decode(cmsData.data));
    return otherTopThirtyBean;
  }

  @override
  Future<ThirdPartyBean> thirdPartyData({maps}) async {
    FormData formData = FormData.fromMap(maps);
    Response<dynamic> cmsData = await dioManager.post(path: Constant.thirdPartyDataUrl, options: Options(headers: {"content-type": "form-data"}), data: formData);
    ThirdPartyBean thirdPartyBean = ThirdPartyBean.fromJson(json.decode(cmsData.data));
    return thirdPartyBean;
  }

  @override
  Future<CmsTicketBean> cmsTicketData({maps}) async {
    FormData formData = FormData.fromMap(maps);
    Response<dynamic> cmsData = await dioManager.post(path: Constant.sameCitySummaryDataUrl, options: Options(headers: {"content-type": "form-data"}), data: formData);
    CmsTicketBean cmcTicketBean = CmsTicketBean.fromJson(json.decode(cmsData.data));
    return cmcTicketBean;
  }

  @override
  Future<DailyVoteBean> dailyVoteData({maps}) async {
    FormData formData = FormData.fromMap(maps);
    Response<dynamic> cmsData = await dioManager.post(path: Constant.dailyVoteDataUrl, options: Options(headers: {"content-type": "form-data"}), data: formData);
    DailyVoteBean dailyVoteBean = DailyVoteBean.fromJson(json.decode(cmsData.data));
    return dailyVoteBean;
  }

  @override
  Future<DailyInformationBean> dailyInfoData({maps}) async {
    FormData formData = FormData.fromMap(maps);
    Response<dynamic> dailyInfoData = await dioManager.post(path: Constant.dailyInfoDataUrl, options: Options(headers: {"content-type": "form-data"}), data: formData);
    DailyInformationBean dailyInformationBean; // = DailyInformationBean.fromJson(json.decode(cmsData.data));
    dailyInformationBean = DailyInformationBean.fromJson(json.decode(dailyInfoData.data));

    dailyInformationBean.data = dailyInformationBean.data?.where((element) => element.bLCOST != 0).toList();
    dailyInformationBean.data = dailyInformationBean.data?.where((element) => element.bGNAME != "测试公司群组").toList();
    var data = dailyInformationBean.data;
    data?.sort((a, b) {
      return a.bLCOST!.compareTo(b.bLCOST!);
    });
    data?.sort((a, b) {
      return a.bGNAME!.compareTo(b.bGNAME!);
    });
    data?.sort((a, b) {
      return a.bLAPPLYDATE!.compareTo(b.bLAPPLYDATE!);
    });

    dailyInformationBean.data = data;
    return dailyInformationBean;
  }

  @override
  Future<SignStatisticsBean> querySignStatisticsForDataUrl({maps}) async {
    printRed("请求数据 $maps");
    FormData formData = FormData.fromMap(maps);
    SignStatisticsBean signStatisticsBean;
    try{

      Response<dynamic> dailyInfoData = await dioManager.post(path: Constant.querySignForDataUrl, options: Options(headers: {"content-type": "form-data"}), data: formData);
      signStatisticsBean = SignStatisticsBean.fromJson(json.decode(dailyInfoData.data), reqCodes: {"compCode": maps["compCode"], "empCode": maps["empCode"]});
    }catch(e){
      print(e);
      signStatisticsBean = SignStatisticsBean(msg: "获取数据失败", status: 0, data: []);
    }
    return signStatisticsBean;
  }

  @override
  Future<CustomerBean> queryCustomerData({maps}) async {
    FormData formData = FormData.fromMap(maps);
    Response<dynamic> dailyInfoData = await dioManager.post(path: Constant.queryCustomerDataUrl, options: Options(headers: {"content-type": "form-data"}), data: formData);
    CustomerBean customerBean;
    customerBean = CustomerBean.fromJson(json.decode(dailyInfoData.data));
    return customerBean;
  }

  @override
  Future<CustomerAdditionBean> customerAdditionTerminateReportData({maps}) async {
    FormData formData = FormData.fromMap(maps);
    Response<dynamic> dailyInfoData = await dioManager.post(path: Constant.customerAdditionTerminateReportUrl, options: Options(headers: {"content-type": "form-data"}), data: formData);
    CustomerAdditionBean customerAdditionBean;
    customerAdditionBean = CustomerAdditionBean.fromJson(json.decode(dailyInfoData.data));
    return customerAdditionBean;
  }

  @override
  Future<OtherOnlineRetailersBean> otherOnlineRetailersComparisonData({maps}) async {
    FormData formData = FormData.fromMap(maps);
    Response<dynamic> dailyInfoData = await dioManager.post(path: Constant.otherOnlineRetailersComparisonUrl, options: Options(headers: {"content-type": "form-data"}), data: formData);
    OtherOnlineRetailersBean otherOnlineRetailersBean = OtherOnlineRetailersBean.fromJson(json.decode(dailyInfoData.data), month: maps["month"]);
    return otherOnlineRetailersBean;
  }

  @override
  Future<AccountsReceivableBean> accountsReceivableInfoData({maps}) async {
    FormData formData = FormData.fromMap(maps);
    Response<dynamic> dailyInfoData = await dioManager.post(path: Constant.accountsReceivableInfoUrl, options: Options(headers: {"content-type": "form-data"}), data: formData);
    AccountsReceivableBean accountsReceivableBean = AccountsReceivableBean.fromJson(json.decode(dailyInfoData.data));
    return accountsReceivableBean;
  }

  @override
  Future<AdjustInvoiceBean> adjustInvoiceData({maps}) async {
    FormData formData = FormData.fromMap(maps);
    Response<dynamic> dailyInfoData = await dioManager.post(path: Constant.adjustInvoiceUrl, options: Options(headers: {"content-type": "form-data"}), data: formData);
    AdjustInvoiceBean adjustInvoiceBean = AdjustInvoiceBean.fromJson(json.decode(dailyInfoData.data));
    return adjustInvoiceBean;
  }
}
